//
//  DeviceEditVC.m
//  FishTank
//
//  Created by Scott Zhu on 14/12/30.
//  Copyright (c) 2014年 Scott Zhu. All rights reserved.
//

#import "DeviceEditVC.h"
#import "APIs.h"


@interface DeviceEditVC ()

@end

@implementation DeviceEditVC
@synthesize deviceInfo = _deviceInfo;

-(void) fixDevicePress:(UILongPressGestureRecognizer *) sender{
    
     if (sender.state == UIGestureRecognizerStateBegan) {
         FixDeviceVC * vc = [[FixDeviceVC alloc] init];
         vc.did = _deviceInfo.did;
         vc.psw = _deviceInfo.password;
         [self.navigationController pushViewController:vc animated:YES];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.renameTextField.placeholder = _deviceInfo.name;
    [self.deviceName setText:_deviceInfo.name];
    [self.deviceUid setText:[NSString stringWithFormat:@"DID：%@", _deviceInfo.did]];
    
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
                                                      initWithTarget:self
                                                      action:@selector(fixDevicePress:)];
    [longPressGesture setMinimumPressDuration:3];
    
    
    [self.navigationController.view addGestureRecognizer:longPressGesture];
    
    self.deviceUid.adjustsFontSizeToFitWidth = YES;
    [self _initDeleteButton];
    [self createRightBarButtonItemWithTitle:@"完成"];
    
    self.lblPassword.text = _deviceInfo.password;
    [self.lockDeviceSwith addTarget:self action:@selector(switchPassword) forControlEvents:UIControlEventValueChanged];
    [self switchPassword];
}

-(void) switchPassword{
    
    if(self.lockDeviceSwith.on){
        self.lblPassword.text = self.deviceInfo.password;
    }else{
        self.lblPassword.text = @"********";
    }
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    
}




-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
}






- (void)_initDeleteButton{
    // 创建删除设备按钮
    UIButton  * deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:deleteBtn];
    [deleteBtn setTitle:@"删除设备" forState:UIControlStateNormal];
    [deleteBtn setTintColor:[UIColor blackColor]];
    [deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    [deleteBtn setBackgroundColor:[UIColor redColor]];
    deleteBtn.layer.borderColor = [UIColor redColor].CGColor;
    deleteBtn.layer.borderWidth = 1;
    deleteBtn.layer.masksToBounds = YES;
    deleteBtn.layer.cornerRadius = 5.0;
    
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.renameTextField.mas_bottom).offset(100);
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.equalTo(@40);
        make.width.equalTo(@(SCREEN_WIDTH - 60));
    }];

}


- (void)deleteAction:(UIButton *)button{
    DeviceListVC *deviceListVC = (DeviceListVC *)[self.navigationController.viewControllers objectAtIndex:0];
    [deviceListVC.deviceList removeObject:_deviceInfo];
    [deviceListVC.tableView reloadData];
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSData * data =  [NSKeyedArchiver archivedDataWithRootObject:deviceListVC.deviceList];
    [userDefaults  setObject:data forKey:SENSEN_DEVICE_AQ];//存储设备列表
    [userDefaults synchronize];
    [SVProgressHUD showSuccessWithStatus:@"设备删除成功" withDuration:1];
    [self.navigationController popViewControllerAnimated:YES];

}



// 创建导航栏右侧的按钮，样式是文字的
- (void)createRightBarButtonItemWithTitle:(NSString *)title {
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton setTitleColor:rgba(0, 121, 255, 1) forState:UIControlStateNormal];
    rightButton.titleLabel.font = FONT(15);
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}


- (void)rightAction:(UIButton *)button{
    
    DeviceListVC *deviceListVC = (DeviceListVC *)[self.navigationController.viewControllers objectAtIndex:0];
    DeviceInfo *deviceInfo = [deviceListVC.deviceList objectAtIndex:_deviceInfo.indexPath.section];
    if (self.renameTextField.text.length == 0 || self.renameTextField.text == nil || self.renameTextField == nil) {
        self.renameTextField.text = _deviceInfo.name;
        self.deviceName.text = _deviceInfo.name;
    }else{
       [SVProgressHUD showSuccessWithStatus:@"设备名称修改成功" withDuration:1];
    }
    [deviceInfo setName:self.renameTextField.text];
    [deviceListVC.tableView reloadData];
    
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSData * data =  [NSKeyedArchiver archivedDataWithRootObject:deviceListVC.deviceList];
    [userDefaults  setObject:data forKey:SENSEN_DEVICE_AQ];//存储设备列表
    [userDefaults synchronize];
    [self.navigationController popViewControllerAnimated:YES];
    
}




#pragma -mark- UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (self.renameTextField == textField)
    {
        [self.renameTextField resignFirstResponder];
        return NO;
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
